# -*- coding: utf-8 -*-
"""
------------------------------------------------------------------------------
    File Name:  file_utils
    Author   :  wanwei1029
    Date     :  2019/5/4
    Desc     :
------------------------------------------------------------------------------
"""
import os
import samp.samp_logging as sl
import samp.utils.string_utils as strutils


log = sl.get_logger("file_utils")


def read_file_to_list( filepath ):
    """
    将单篇文档，按行读取返回一个List
    :param filepath:
    :return:
    """
    result = list()
    with open(filepath, "r+", encoding="utf-8") as file:
        for line in file.readlines():
            result.append(line.strip())
    return result


def read_dir_to_list(dir_path):
    """
    将目录下所有的文档读取并返回一个List,一个元素就是一篇文档。
    :param dir_path:目录地址
    :return: list
    """
    result = list()
    doc_lists = os.listdir(dir_path)
    log.debug(doc_lists)
    for doc in doc_lists:
        filepath = dir_path+os.path.sep+doc
        if not os.path.isdir(filepath):
            log.info("load file:{0}".format(filepath))
            with open(filepath, "r+", encoding="utf-8") as file:
                dos_data = ""
                for line in file.readlines():
                    if strutils.is_not_blank(line):
                        dos_data += line.strip()+"#"
                log.debug("add content:{0}".format(dos_data))
                result.append(dos_data)
    return result


def read_file_to_str(filepath):
    with open(filepath, "r+", encoding="utf-8") as file:
        dos_data = ""
        for line in file.readlines():
            if strutils.is_not_blank(line):
                dos_data += line.strip() + "#"
        return dos_data


def demo():
    """
    """


if __name__ == '__main__':
    test_method = "read_dir_to_list"
    if test_method == "demo":
        demo()
    elif test_method == "read_file_to_list":
        filepath = "D:\\nas\\nlp\\keywords\\text01_tokenize.txt"
        datas = read_file_to_list(filepath)
        print(datas)
    elif test_method == "read_dir_to_list":
        path = "D:\\nas\\nlp\\keywords\\zaobao\\processed"
        read_dir_to_list(path)
